<html>
<head>

<title>Groovy Goodness: Splitting Strings</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Splitting Strings</h3>

<div class="post">
<p>In Java we can use the <code>split()</code> method of the <code>String</code> class or the <code>StringTokenizer</code> class to split strings. Groovy adds the methods <code>split()</code> and <code>tokenize()</code> to the <code>String</code> class, so we can invoke them directly on a string. The <code>split()</code> method return a <code>String[]</code> instance and the <code>tokenize()</code> method return a <code>List</code>. There is also a difference in the argument we can pass to the methods. The <code>split()</code> method takes a regular expression string and the <code>tokenize()</code> method will use all characters as delimiter.</p>
<pre class="brush:groovy">
def s = '''\
username;language,like
mrhaki,Groovy;yes
'''

assert s.split() instanceof String[]
assert ['username;language,like', 'mrhaki,Groovy;yes'] == s.split()  // Default split on whitespace. ( \t\n\r\f)
assert ['username', 'language', 'like', 'mrhaki', 'Groovy', 'yes'] == s.split(/(;|,|\n)/)  // Split argument is a regular expression.

def result = []
s.splitEachLine(&quot;,&quot;) {
    result &lt;&lt; it  // it is list with result of split on ,
}
assert ['username;language', 'like'] == result[0]
assert ['mrhaki', 'Groovy;yes'] == result[1]

assert s.tokenize() instanceof List
assert ['username;language,like', 'mrhaki,Groovy;yes'] == s.tokenize()  // Default tokenize on whitespace. ( \t\n\r\f)
assert ['username', 'language', 'like', 'mrhaki', 'Groovy', 'yes'] == s.tokenize(&quot;\n;,&quot;)  // Argument is a String with all tokens we want to tokenize on.
</pre>
<p>Run script on <a href="http://groovyconsole.appspot.com/view.groovy?id=34003">Groovy web console</a>.</p
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>